/**
  ******************************************************************************
  * @file    usart.c
  * @brief   This file provides code for the configuration
  *          of the USART instances.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under Ultimate Liberty license
  * SLA0044, the "License"; You may not use this file except in compliance with
  * the License. You may obtain a copy of the License at:
  *                             www.st.com/SLA0044
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stdarg.h"
/* USER CODE BEGIN 0 */
u32 UART1TimeOut=0;
Uart_RcvTypedef Uart1Buf;
/* USER CODE END 0 */

/* USART1 init function */

void MX_USART1_UART_Init(void)
{

  /* USER CODE BEGIN USART1_Init 0 */

  /* USER CODE END USART1_Init 0 */

  LL_USART_InitTypeDef USART_InitStruct = {0};

  LL_GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* Peripheral clock enable */
  LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_USART1);

  LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA);
  /**USART1 GPIO Configuration
  PA9   ------> USART1_TX
  PA10   ------> USART1_RX
  */
  GPIO_InitStruct.Pin = LL_GPIO_PIN_9;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
  GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  LL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  GPIO_InitStruct.Pin = LL_GPIO_PIN_10;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_FLOATING;
  LL_GPIO_Init(GPIOA, &GPIO_InitStruct);

//  /* USART1 interrupt Init */
//  NVIC_SetPriority(USART1_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),5, 0));
//  NVIC_EnableIRQ(USART1_IRQn);

//  /* USER CODE BEGIN USART1_Init 1 */
//	LL_USART_EnableIT_RXNE(USART1);
//	LL_USART_EnableIT_IDLE(USART1);
//  /* USER CODE END USART1_Init 1 */
  USART_InitStruct.BaudRate = 115200;
  USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B;
  USART_InitStruct.StopBits = LL_USART_STOPBITS_1;
  USART_InitStruct.Parity = LL_USART_PARITY_NONE;
  USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX_RX;
  USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_NONE;
  USART_InitStruct.OverSampling = LL_USART_OVERSAMPLING_16;
  LL_USART_Init(USART1, &USART_InitStruct);
  LL_USART_ConfigAsyncMode(USART1);
  LL_USART_Enable(USART1);
  /* USER CODE BEGIN USART1_Init 2 */

  /* USER CODE END USART1_Init 2 */

}

/* USER CODE BEGIN 1 */
void Uart1_SendByte(uint8_t *txdBuf,uint16_t len)
{
	u32 i= 0;
	RS485_RxDen();
//	osDelay(1);
	for( i = 0; i < len; i++) 
	{
		UART1TimeOut= 0;
		USART1->DR = ( u8 ) * txdBuf++;
		while( (( USART1->SR & 0X40 ) == 0 ) && ( UART1TimeOut<10 ) );
	}
//	osDelay(1);
	RS485_RxEn();
}

void rf_puts( const char* str )
{
	while ( *str != 0 )
	{
		UART1TimeOut= 0;
		while( (( USART1->SR & 0X40 ) == 0 ) && ( UART1TimeOut<10 ) );
		USART1->DR = ( u8 ) * str++;
	}
}


uint8_t rf_itoa( int32_t val, int radix, int len, char* sout, unsigned char ptr )
{
	uint8_t c, r, sgn = 0, pad = ' ';
	uint8_t s[20], i = 0;
	uint32_t v;

	if ( radix < 0 )
	{
		radix = -radix;

		if ( val < 0 )
		{
			val = -val;
			sgn = '-';
		}
	}

	v = val;
	r = radix;

	if ( len < 0 )
	{
		len = -len;
		pad = '0';
	}

	if ( len > 20 )
	{
		return ptr;
	}

	do
	{
		c = ( uint8_t )( v % r );

		if ( c >= 10 )
		{
			c += 7;
		}

		c += '0';
		s[i++] = c;
		v /= r;
	}
	while ( v );

	if ( sgn )
	{
		s[i++] = sgn;
	}

	while ( i < len )
	{
		s[i++] = pad;
	}

	do
	{
		sout[ptr++] = ( s[--i] );
	}
	while ( i );

	return ptr;
}

void dbg_printf( const char* str, ... )
{
	va_list arp;
	int d, r, w, s, l;
	char sout[256];
	unsigned char ptr = 0;

	va_start( arp, str );
	RS485_RxDen();
	osDelay(1);

	while ( ( d = *str++ ) != 0 )
	{
		if ( d != '%' )
		{
			sout[ptr++] = d;
			continue;
		}

		d = *str++;
		w = r = s = l = 0;

		if ( d == '0' )
		{
			d = *str++;
			s = 1;
		}

		while ( ( d >= '0' ) && ( d <= '9' ) )
		{
			w += w * 10 + ( d - '0' );
			d = *str++;
		}

		if ( s )
		{
			w = -w;
		}

		if ( d == 'l' )
		{
			l = 1;
			d = *str++;
		}

		if ( !d )
		{
			break;
		}

		if ( d == 's' )
		{
			char* s = va_arg( arp, char* );

			while ( *s != 0 )
			{
				sout[ptr++] = *s;
				s++;
			}

			continue;
		}

		if ( d == 'c' )
		{
			sout[ptr++] = ( char )va_arg( arp, int );
			continue;
		}

		if ( d == 'u' )
		{
			r = 10;
		}

		if ( d == 'd' )
		{
			r = -10;
		}

		if ( d == 'X' || d == 'x' )
		{
			r = 16;    // 'x' added by mthomas in increase compatibility
		}

		if ( d == 'b' )
		{
			r = 2;
		}

		if ( !r )
		{
			break;
		}

		if ( l )
		{
			ptr = rf_itoa( ( long )va_arg( arp, long ), r, w, sout, ptr );
		}
		else
		{
			if ( r > 0 )
			{
				ptr = rf_itoa( ( unsigned long )va_arg( arp, int ), r, w, sout, ptr );
			}
			else
			{
				ptr = rf_itoa( ( long )va_arg( arp, int ), r, w, sout, ptr );
			}
		}
	}

	va_end( arp );
	sout[ptr] = 0;
	rf_puts( sout );
	
	RS485_RxEn();
	osDelay(2);
}





/* USER CODE END 1 */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
